############################################################################
#
# ds.pm
#
# Directory Server script support module
#
# Copyright (C) 2006 The University of Chicago
#
# Darren Young <darren.young@gsb.uchicago.edu>
#
# $Id: ds.pm,v 1.1 2006/07/12 18:37:41 dyoung2 Exp $
#
############################################################################
#
# NOTICE
# ------
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
# USA.
#
############################################################################
#
# CHANGELOG
# ---------
# 
# $Log: ds.pm,v $
# Revision 1.1  2006/07/12 18:37:41  dyoung2
#   * Added from idstools
#
# Revision 1.3  2006/07/06 03:23:17  root
#   * Moved logging functions to logging.pm
#
# Revision 1.2  2006/06/08 00:52:29  root
#   * Added POSIX module.
#
# Revision 1.1.1.1  2006/06/02 23:05:25  root
#   * Initial import to CVS
#
############################################################################
#
# NOTES:
# ------
#
############################################################################

my  $cvsid   = '$Id: ds.pm,v 1.1 2006/07/12 18:37:41 dyoung2 Exp $';
my  @cvsinfo = split( ' ', $cvsid );
our $NAME    = File::Basename::basename($0);
our $VERSION = $cvsinfo[2];


# ------------------------------------------------------------------------------
#                             B E G I N
# ------------------------------------------------------------------------------
BEGIN {

    # Pragmas
    use strict;

    # "Standard" modules we use
    use FindBin;
    use Config::Simple;
    use POSIX qw(strftime);
    use lib "$FindBin::Bin/../lib";

    # Standard account maint modules to use
    use logging;
    use errcodes;
    use utils;
    
    # Modules that this module uses
    use Sys::Hostname;
    use Carp;

}


# ------------------------------------------------------------------------------ 
#                             V A R I A B L E S
# ------------------------------------------------------------------------------ 
our $LOGFILE    = "$FindBin::Bin/../log/acctmgr.log" unless($LOGFILE); 
our $CONFIGFILE = "$FindBin::Bin/../etc/acctmgr.cfg" unless($CONFIGFILE); 


# ------------------------------------------------------------------------------
#                             C O N F I G   F I L E
# ------------------------------------------------------------------------------
our $CFG = new Config::Simple( filename => $CONFIGFILE ) or die "$!\n" unless($CFG);


# ##########################################################################
# NAME        : get_mps_root
# DESCRIPTION : get the root installation directory for the mps stuff
# ARGUMENTS   : scalar(file)
# RETURN      : True
# NOTES       : 
# ##########################################################################
sub get_mps_root {
    my ($regfile) = @_;
    my $rootpath = "";

    if ( open(REGFILE, "<$regfile") ) {
        while(<REGFILE>) {
            next if /^#/;   # skip comments
            next if /^$/;   # skip blank lines
            chomp;

            # grab the first line, read it then leave
            $rootpath = $_;
            last;
        }

        return($rootpath);

    } else {
        logmsg("FATAL: failed to open regfile: $regfile");
        return(0);
    }


}


# ##########################################################################
# NAME        : get_mps_root
# DESCRIPTION : get the root installation directory for the mps stuff
# ARGUMENTS   : scalar(directory)
# RETURN      : array(datbases)
# NOTES       : 
# ##########################################################################
sub get_ldap_databases {
    my ($dbdir) = @_;
    my @databases;

    opendir(DBDIR, $dbdir) || die "FAILED to open $dbdir ($!)";
    while (my $n = readdir(DBDIR)) {
        if ( $n eq "\." || $n eq "\.." ) { next; }
        if ( -d "$dbdir/$n" ) {
            debug("got raw database dir: $n");
            push(@databases, URLDecode($n));
        }
    }
    closedir(DBDIR);

    return(@databases);
}


# ##########################################################################
# NAME        : URLDecode
# DESCRIPTION : Decode an en encoded URL
# ARGUMENTS   : scalar(url)
# RETURN      : scalar(url)
# NOTES       : 
# ##########################################################################
sub URLDecode {
    my ($URL) = @_;
    $URL =~ tr/+/ /;
    $URL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
    $URL =~ s/<!--(.|\n)*-->//g;
    return($URL);
}


# ##########################################################################
# NAME        : get_ds_account
# DESCRIPTION : Retrieves the dn and password for a ds account
# ARGUMENTS   : scalar(admpwfile), scalar(identifier)
# RETURN      : array[0]->dn
#             : array[1]->password
# NOTES       : 
# ##########################################################################
sub get_ds_account {
    my ($admpwfile, $id) = @_;
    my $x;
    my $dn;
    my $passwd;
    my @ret;

    open(ADMPWFILE, "<$admpwfile") or die "failed to open $admpwfile";
    while(<ADMPWFILE>) {
        next if /^#/;
        next if /^$/;
        chomp;

        if ( /^$id:/ ) {
            debug("found id $id in admpwfile");
            ($x, @ret[0], @ret[1]) = split(':', $_);
            last;
        }
    }
    close(ADMPWFILE);
    return(@ret);
}

# return true
1;
